package com.example.snowli01.fragments;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.snowli01.EditActivity;
import com.example.snowli01.MainActivity;
import com.example.snowli01.R;
import com.example.snowli01.adapter.ShowWordsAdapter;

import java.util.ArrayList;
import java.util.List;

public class WordsFragment extends Fragment {
    private SQLiteDatabase db;


    private TextView showNull;
    private ListView listWords;
    //listview 适配器
    private ShowWordsAdapter showWordsAdapter;

    private List<String> wordsList;



    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View wordPage = inflater.inflate(R.layout.activity_words_fragment,null);

        db = MainActivity.db;
        listWords = wordPage.findViewById(R.id.list_words);
        showNull = wordPage.findViewById(R.id.null_show);

        selectWords();


        //点击单词列表跳转到新页面，对单词进行编辑
        listWords.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent();
                intent.setClass(
                        getActivity(),
                        EditActivity.class
                );
                String wordAndTran = wordsList.get(position);
                intent.putExtra("wordAndT",wordAndTran);

                startActivity(intent);


            }
        });





        return wordPage;

    }

    private void selectWords() {
        //查询数据
        Cursor cursor = db.query("words",null,null,null,null,null,null);
        wordsList = new ArrayList<>();
        while (cursor.moveToNext()){
            int idindex = cursor.getColumnIndex("id");
            int id = cursor.getInt(idindex);
            int wordIndex = cursor.getColumnIndex("word");
            String word = cursor.getString(wordIndex);
            int tranIndex = cursor.getColumnIndex("tran");
            String tran = cursor.getString(tranIndex);
            wordsList.add(id+":"+word+":"+tran);
        }
        System.out.println(wordsList.toString());
        if (wordsList.isEmpty()){
            showNull.setText("暂无单词");
        }else{
            showNull.setText("");
        }


        //绑定适配器，放入listview中
        //实例化适配器对象
        showWordsAdapter = new ShowWordsAdapter(
                getActivity(),
                R.layout.activity_select_item,
                wordsList
        );
        //绑定适配器
        listWords.setAdapter(showWordsAdapter);


    }


    @Override
    public void onResume(){
        super.onResume();
        selectWords();
    }


}

